package com.robot.agent.service;

import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.robot.agent.dao.AgentContractDao;
import com.robot.agent.pojo.AgentContract;

/**
 * agentContract服务层
 *
 * @author wuyunbin
 */
@Service
public class AgentContractService {

    @Autowired
    private AgentContractDao agentContractDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<AgentContract> findAll() {
        return agentContractDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<AgentContract> findSearch(Map whereMap, int page, int size) {
        Specification<AgentContract> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return agentContractDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<AgentContract> findSearch(Map whereMap) {
        Specification<AgentContract> specification = createSpecification(whereMap);
        return agentContractDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public AgentContract findById(String id) {
        return agentContractDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param agentContract
     */
    public void add(AgentContract agentContract) {
        //雪花分布式ID生成器
        agentContract.setId(idWorker.nextId() + "");
        agentContractDao.save(agentContract);
    }

    /**
     * 修改
     *
     * @param agentContract
     */
    public void update(AgentContract agentContract) {
        agentContractDao.save(agentContract);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        agentContractDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<AgentContract> createSpecification(Map searchMap) {
        return new Specification<AgentContract>() {
            @Override
            public Predicate toPredicate(Root<AgentContract> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 合同Id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 联系电话
                String phone = "phone";
                if (searchMap.get(phone) != null && !"".equals(searchMap.get(phone))) {
                    predicateList.add(cb.like(root.get(phone).as(String.class), "%" + (String) searchMap.get(phone) + "%"));
                }
                // 合同版本号
                String contractVersion = "contractVersion";
                if (searchMap.get(contractVersion) != null && !"".equals(searchMap.get(contractVersion))) {
                    predicateList.add(cb.like(root.get(contractVersion).as(String.class), "%" + (String) searchMap.get(contractVersion) + "%"));
                }
                // 签订人
                String signer = "signer";
                if (searchMap.get(signer) != null && !"".equals(searchMap.get(signer))) {
                    predicateList.add(cb.like(root.get(signer).as(String.class), "%" + (String) searchMap.get(signer) + "%"));
                }
                // 代理商姓名
                String realName = "realName";
                if (searchMap.get(realName) != null && !"".equals(searchMap.get(realName))) {
                    predicateList.add(cb.like(root.get(realName).as(String.class), "%" + (String) searchMap.get(realName) + "%"));
                }
                // 代理商类型 0-私人 1-公司
                String agentType = "agentType";
                if (searchMap.get(agentType) != null && !"".equals(searchMap.get(agentType))) {
                    predicateList.add(cb.like(root.get(agentType).as(String.class), "%" + (String) searchMap.get(agentType) + "%"));
                }
                // 公司名称
                String companyName = "companyName";
                if (searchMap.get(companyName) != null && !"".equals(searchMap.get(companyName))) {
                    predicateList.add(cb.like(root.get(companyName).as(String.class), "%" + (String) searchMap.get(companyName) + "%"));
                }

                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(Date.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

}
